package com.estronger.shopmanage.adapter;

import android.app.Dialog;
import android.content.Context;
import android.os.Handler;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;


;import com.android.tu.loadingdialog.LoadingDailog;
import com.estronger.shopmanage.R;
import com.estronger.shopmanage.base.RecyclerBaseAdapter;
import com.estronger.shopmanage.ui.othertask.bean.FileBean;
import com.estronger.shopmanage.utils.OpenFileUtil;
import com.soonfor.evaluate.tools.ImageUtils;

import java.util.List;

/**
 * 作者：DC-DingYG on 2018/7/20 13:50
 * 邮箱：dingyg012655@126.com
 * 回复评论的回复
 */
public class ItemFileAdapter extends RecyclerBaseAdapter<ItemFileAdapter.ViewHolder, FileBean> {

    private List<FileBean> list;
    private Context mContext;
    private LoadingDailog mLoadingDialog;
    private OnItemClick onItemClick;
    private boolean isShowDelete = false;

    public ItemFileAdapter(Context context, List<FileBean> list, LoadingDailog mLoadingDialog, boolean isShowDelete) {
        super(context);
        mContext = context;
        this.list = list;
        this.mLoadingDialog = mLoadingDialog;
        this.isShowDelete = isShowDelete;
    }

    public void setOnItemClick(OnItemClick onItemClick) {
        this.onItemClick = onItemClick;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(mInflater.inflate(R.layout.adpter_item_file, parent, false));
    }

    Dialog mDialog;

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.setData(list.get(position));
        holder.tvfFileName.setTag(R.id.file_position, position);
        holder.tvfFileName.setTag(R.id.file_viewholder, holder);
        holder.tvfDeleteFile.setTag(R.id.file_position, position);
        holder.tvfFileName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                int pos = (int) v.getTag(R.id.file_position);
                OpenFileUtil.show(context, list.get(pos), new OpenFileUtil.CallBack() {
                    @Override
                    public void startLoadingDialog() {
                        showLoadingDialog();
                    }

                    @Override
                    public void onDownloadProgress() {

                    }

                    @Override
                    public void endLoadingDialog() {
                        closeLoadingDialog();
                    }
                });
            }
        });
        holder.tvfDeleteFile.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClick != null) {
                    int pos = (int) v.getTag(R.id.file_position);
                    if (pos >= 0 && pos < list.size()) {
                        onItemClick.onLongClickListener(v, list, pos);
                    }
                }
            }
        });
    }

    @Override
    public void notifyDataSetChanged(List<FileBean> dataList) {
        this.list = dataList;
        notifyDataSetChanged();
    }

    public List<FileBean> getList() {
        return list;
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvfFileName;
        TextView tvfDeleteFile;

        public ViewHolder(View itemView) {
            super(itemView);
            tvfFileName = itemView.findViewById(R.id.item_others_name);
            tvfDeleteFile = itemView.findViewById(R.id.item_deleteFile);
        }

        public void setData(FileBean gpBean) {
            if (isShowDelete) {
                tvfDeleteFile.setVisibility(View.VISIBLE);
            } else {
                tvfDeleteFile.setVisibility(View.GONE);
            }
            tvfFileName.setText(gpBean.getAttachName());
        }
    }


    public void showLoadingDialog() {
        if (mLoadingDialog != null && !mLoadingDialog.isShowing()) {
            mLoadingDialog.show();
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    closeLoadingDialog();
                }
            }, 10000);
        }
    }

    public void closeLoadingDialog() {
        if (mLoadingDialog != null && mLoadingDialog.isShowing())
            mLoadingDialog.dismiss();
    }

    public interface OnItemClick {
        void onLongClickListener(View view, List<FileBean> fileBeans, int adapterposition);
    }
}
